package com.talent.study.kafka;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.util.Collections;
import java.util.Properties;

/**
 * @Author: hszhou
 * @Date: 2025/5/9 9:27
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
public class TestConsumer {


    public static void main(String[] args) {


        Properties properties =  new Properties();

        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"172.28.33.102:9092");
        properties.put(ConsumerConfig.GROUP_ID_CONFIG,"test-group");
        properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,true);
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserializer");
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserializer");
        properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");


       KafkaConsumer<String,String> consumer = new KafkaConsumer<>(properties);

        consumer.subscribe(Collections.singletonList("ws"));

        // 持续轮询消息
        while (true){
            // 每次轮询的超时时间（毫秒）
            ConsumerRecords<String, String> records = consumer.poll(1000);
            for (ConsumerRecord<String, String> record : records) {
                System.out.printf(
                        "Received message -> offset = %d, key = %s, value = %s, partition = %d%n",
                        record.offset(),
                        record.key(),
                        record.value(),
                        record.partition()
                );

                // 这里添加您的业务逻辑处理代码...
            }
        }
    }
}
